package com.molichuxing.services.business.service.impl;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.services.business.dto.request.create.EnterpriseCarTransferCreateBizDto;
import com.molichuxing.services.business.service.EnterpriseCarTransferBizService;
import com.molichuxing.services.business.service.EnterpriseScheduleBizService;
import com.molichuxing.services.infrastructure.dto.request.create.EnterpriseCarTransferCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EnterpriseCarModifyDto;
import com.molichuxing.services.infrastructure.service.EnterpriseCarService;
import com.molichuxing.services.infrastructure.service.EnterpriseCarTransferService;

/**
 * 企业车辆过户组合服务实现类
 *
 * @author liyiguang
 * @date 2020年6月4日
 */
@Service("enterpriseCarTransferBizService")
public class EnterpriseCarTransferBizServiceImpl implements EnterpriseCarTransferBizService {

	
	@Resource
	private EnterpriseCarTransferService enterpriseCarTransferService;
	
	@Resource
	private EnterpriseCarService enterpriseCarService;
	
	@Resource
	private EnterpriseScheduleBizService enterpriseScheduleBizService;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	public Boolean create(EnterpriseCarTransferCreateBizDto createBizDto) {
	
		//保存过户信息
		EnterpriseCarTransferCreateDto transferCreateDto =new EnterpriseCarTransferCreateDto();
		BeanUtils.copyProperties(createBizDto, transferCreateDto);
		Boolean flag = enterpriseCarTransferService.create(transferCreateDto);
		if(!flag) {
			return flag;
		}
		//订单车辆状态置为“已过户”
		EnterpriseCarModifyDto modifyDto =new EnterpriseCarModifyDto();
		BeanUtils.copyProperties(createBizDto, modifyDto);
		if(modifyDto.getEndMileage() == null) {
			modifyDto.setEndMileage(0);
		}
		flag= enterpriseCarService.modifyTransfer(modifyDto);
		if(!flag) {
			throw new RuntimeException("订单车辆状态置为“已过户” 失败");
		}
		//终止订单车辆排期
		flag= enterpriseScheduleBizService.modifyCarTermination(createBizDto.getOrderCode(),createBizDto.getCarId());
		if(!flag) {
			throw new RuntimeException("车辆过户 终止订单车辆排期失败");
		}
		return flag;
	}
}
